package com.util.gen;

import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import com.util.RandomUtil;
import com.util.TaskUtil;

import java.io.FileOutputStream;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <p>
 *
 * @author: Yuanwl
 * @date: 2020/12/20 12:27
 */
public class PdfGenUtil {

    private static final String IMG_PATH = ImageGenUtil.class.getResource("/static/bg1.jpg").getPath();
    private static final AtomicInteger PDF_INDEX = new AtomicInteger(0);

    private static String CONTENT = "public abstract class AbstractApplicationContext extends DefaultResourceLoader implements ConfigurableApplicationContext {\n" +
            "    public static final String MESSAGE_SOURCE_BEAN_NAME = \"messageSource\";\n" +
            "    public static final String LIFECYCLE_PROCESSOR_BEAN_NAME = \"lifecycleProcessor\";\n" +
            "    public static final String APPLICATION_EVENT_MULTICASTER_BEAN_NAME = \"applicationEventMulticaster\";\n" +
            "    protected final Log logger;\n" +
            "    private String id;\n" +
            "    private String displayName;\n" +
            "    @Nullable\n" +
            "    private ApplicationContext parent;\n" +
            "    @Nullable\n" +
            "    private ConfigurableEnvironment environment;\n" +
            "    private final List<BeanFactoryPostProcessor> beanFactoryPostProcessors;\n" +
            "    private long startupDate;\n" +
            "    private final AtomicBoolean active;\n" +
            "    private final AtomicBoolean closed;\n" +
            "    private final Object startupShutdownMonitor;\n" +
            "    @Nullable\n" +
            "    private Thread shutdownHook;\n" +
            "    private ResourcePatternResolver resourcePatternResolver;\n" +
            "    @Nullable\n" +
            "    private LifecycleProcessor lifecycleProcessor;\n" +
            "    @Nullable\n" +
            "    private MessageSource messageSource;\n" +
            "    @Nullable\n" +
            "    private ApplicationEventMulticaster applicationEventMulticaster;\n" +
            "    private final Set<ApplicationListener<?>> applicationListeners;\n" +
            "    @Nullable\n" +
            "    private Set<ApplicationEvent> earlyApplicationEvents;";

    public static void main(String[] args) {
        TaskUtil.runMultiTasks(500, "/Volumes/APFS/Downloads/batch/raws", PdfGenUtil::genPdf);
//        TaskUtil.runMultiTasks(500, "D:\\cpz\\raw", PdfGenUtil::genPdf);
    }

    public static void genPdf(String dirPath) {
        int index = PDF_INDEX.addAndGet(1);
        int pageNum = RandomUtil.getRoundRandomInt(3, 8);
        Document document = new Document();

        try {
            PdfWriter.getInstance(document, new FileOutputStream(String.format("%s/%d.pdf", dirPath, index)));
            document.open();

            for (int j = 0; j < pageNum; j++) {
                Paragraph paragraph = new Paragraph();
                if (RandomUtil.getBoolean()) {
                    paragraph.add(CONTENT);
                } else {
                    paragraph.add(Image.getInstance(IMG_PATH));
                }
                document.add(paragraph);

                if (j + 1 < pageNum) {
                    document.newPage();
                }
            }
            document.close();
            System.err.printf("创建第%d个pdf文件完成\n", index);
        } catch (Exception e) {
            System.err.printf("创建第%d个pdf文件失败: %s\n", index, e.getMessage());
            e.printStackTrace();
        }
    }

}
